#pragma once
#include <iostream>
#include <string>
#include <pthread.h>
#include <functional>
namespace threadmoudle
{
    using func_t = std::function<void(const std::string&)>;
    class Thread
    {
    private:
        pthread_t _td;
        std::string _name;
        bool _isrunning;
        func_t _func;
    public:
        void Excute()
        {
            _isrunning=true;
            _func(_name);
            _isrunning=false;
        }
    public:
        Thread(const std::string &name,func_t func):_name(name),_func(func)
        {}
        
        static void *ThreadRountine(void *args)
        {
            Thread *self=static_cast<Thread*>(args);
            self->Excute();
            return nullptr;
        }
        bool Start()
        {
            int n = ::pthread_create(&_td,nullptr,ThreadRountine,this);
            if(n!=0) return false;
            return true;
        }
        std::string Status()
        {
            if(_isrunning) return "running...";
            else return "sleep";
        }
        void Stop()
        {
            if(_isrunning)
            {
                pthread_cancel(_td);
                _isrunning=false;
            }
        }
        void Join()
        {
            pthread_join(_td,nullptr);
        }

        std::string Name()
        {
            return _name;
        }
        ~Thread()
        {
            
        }
    };

}
